import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import learning.xml.jaxb.Artist;
import learning.xml.jaxb.Song;
import learning.xml.jaxb.Score;
import learning.xml.jaxb.Style;

import save.xml.jaxb.Interface;


public class XML {
	public static String ARTIST;
	public static String SONG;
	public static String SCORE;
	public static String STYLE;
	public static JAXBContext artistContext;
	public static JAXBContext songContext;
	public static JAXBContext styleContext;
	public static JAXBContext scoreContext;
	public static Artist artist = new Artist();
	public static Artist artist2 = null;
	public static Style style = new Style();
	public static Song song = new Song();
	public static Score score = new Score();
	
	public static String INTERFACE;
	public static JAXBContext interfaceContext;
	public static Interface _interface = new Interface();

	
// ----------------------- xml Interface info ------------------------------------------------------------------
	
	public void newInterface(String name) throws JAXBException, IOException {
		interfaceContext = JAXBContext.newInstance(Interface.class);
		_interface.setName(name);
		INTERFACE = ("./" + name);
	}
	
	public void setWidth(int width) {
		_interface.setWidth(width);
	}

	public void setHeight(int height) {
		_interface.setHeight(height);
	}

	public void setLocationX(int locationX) {
		_interface.setLocationX(locationX);
	}
	
	public void setLocationY(int locationY) {
		_interface.setLocationY(locationY);
	}

	public void setButtons(Object[][] buttons) {
		_interface.setButtons(buttons);
	}
	
	public void setGraphs(Object[][] graphs) {
		_interface.setGraphs(graphs);
	}
	public void setRadials(Object[][] radials) {
		_interface.setRadials(radials);
	}
	public void writeInterface() throws JAXBException, IOException {
		Marshaller m = interfaceContext.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(_interface, System.out);
		Writer w = null;
		try {
			w = new FileWriter(INTERFACE);
			m.marshal(_interface, w);
		} finally {
			try {
				w.close();
			} catch (Exception e) {
			}
		}
	}

	public boolean readInterface(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		INTERFACE = ("./" + name);
		boolean exists = true;
		try {
			_interface = (Interface) um.unmarshal(new FileReader(
					INTERFACE));

		} catch (FileNotFoundException e) {
			exists = false;
		} catch (JAXBException e) {
			exists = false;
		}
		return exists;
	}
	
	public static int getWidth(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		int width = _interface.getWidth();
		return width;
	}
	public static int getHeight(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		int height = _interface.getHeight();
		return height;
	}
	public static int getLocationX(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		int locationX = _interface.getLocationX();
		return locationX;
	}
	public static int getLocationY(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		int locationY = _interface.getLocationY();
		return locationY;
	}
	public static Object[][] getButtons(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		Object[][] buttons = _interface.getButtons();
		return buttons;
	}
	public static Object[][] getGraphs(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		Object[][] graphs = _interface.getGraphs();
		return graphs;
	}
	public static Object[][] getRadials(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = interfaceContext.createUnmarshaller();
		Interface _interface = null;
		INTERFACE = ("./" + name );
		_interface = (Interface) um.unmarshal(new FileReader(
				INTERFACE));
		
		Object[][] radials = _interface.getRadials();
		return radials;
		
		
	}

//	---------------------- xml Artist info: ---------------------------------------------------------------------
	
	public void newArtist(String name) throws JAXBException, IOException {
		artistContext = JAXBContext.newInstance(Artist.class);

		artist.setName(name);
		ARTIST = ("./" + name + ".xml");
	}
	
//	public void newSongStyle(String name) throws JAXBException, IOException {
//		artistContext = JAXBContext.newInstance(Song.class);
//		song.setName(name);
//		SONG = ("./" + name + ".xml");
//	}


	public void setDuration(int[][] duration) {
		artist.setDuration(duration);
	}
	
	public void setOnsets(int[][] onsets) {
		artist.setOnsets(onsets);
	}

	public void setCount(int count) {
		artist.setCount(count);
	}

	public void setPitch(int[][] pitch) {
		artist.setPitch(pitch);
	}

	public void setVolume(int[][] volume) {
		artist.setVolume(volume);
	}
	
	public void setChordRoots(int[] chordRoots) {
		artist.setChordRoots(chordRoots);
	}
	
	public void setChordQualities(int[] chordQualities) {
		artist.setChordQualities(chordQualities);
	}
	
	public void setChordDurations(int[] durations) {
		artist.setChordDurations(durations);
	}
	
	

	public void writeArtist() throws JAXBException, IOException {
		// create JAXB artistContext and instantiate marshaller
		Marshaller m = artistContext.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(artist, System.out);
		Writer w = null;
		try {
			w = new FileWriter(ARTIST);
			m.marshal(artist, w);
		} finally {
			try {
				w.close();
			} catch (Exception e) {
			}
		}
	}

	public boolean readArtist(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = artistContext.createUnmarshaller();
		ARTIST = ("./" + name + ".xml");
		boolean exists = true;

		try {
			artist2 = (Artist) um.unmarshal(new FileReader(
					ARTIST));

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			exists = false;
			// e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			exists = false;
			// e.printStackTrace();
		}
		return exists;
	}
	
	public static boolean readArtistStatic(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = artistContext.createUnmarshaller();
		ARTIST = ("./" + name + ".xml");
		boolean exists = true;

		try {
			artist2 = (Artist) um.unmarshal(new FileReader(
					ARTIST));

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			exists = false;
			// e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			exists = false;
			// e.printStackTrace();
		}
		return exists;
	}

	public static int[][] getPitch(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = artistContext.createUnmarshaller();
		Artist artist2 = null;
		ARTIST = ("./" + name + ".xml");
		try {
			artist2 = (Artist) um.unmarshal(new FileReader(
					ARTIST));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[][] notes = artist2.getPitch();
		return notes;
	}

	public static int[][] getVolume(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = artistContext.createUnmarshaller();
		Artist artist2 = null;
		ARTIST = ("./" + name + ".xml");
		try {
			artist2 = (Artist) um.unmarshal(new FileReader(
					ARTIST));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[][] volume = artist2.getVolume();
		return volume;
	}

	public static int[][] getDuration(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = artistContext.createUnmarshaller();
		Artist artist2 = null;
		ARTIST = ("./" + name + ".xml");
		try {
			artist2 = (Artist) um.unmarshal(new FileReader(
					ARTIST));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[][] duration = artist2.getDuration();
		return duration;
	}
	
	public static int[][] getOnsets(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = artistContext.createUnmarshaller();
		Artist artist2 = null;
		ARTIST = ("./" + name + ".xml");
		try {
			artist2 = (Artist) um.unmarshal(new FileReader(
					ARTIST));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[][] onsets = artist2.getOnsets();
		return onsets;
	}

	public static int getCount(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = artistContext.createUnmarshaller();
		Artist artist2 = null;
		ARTIST = ("./" + name + ".xml");
		try {
			artist2 = (Artist) um.unmarshal(new FileReader(
					ARTIST));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int count = artist2.getCount();
		return count;
	}

	public void init() throws JAXBException, IOException {
		newArtist("default");
		newScore("defaultScore");
		newSong("defaultSong");
	}
	
	public static int[] getChordRoots(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = artistContext.createUnmarshaller();
		Artist artist2 = null;
		ARTIST = ("./" + name + ".xml");
		artist2 = (Artist) um.unmarshal(new FileReader(
				ARTIST));
		
		int[] chordRoots = artist2.getChordRoots();
		return chordRoots;
	}
	
	public static int[] getChordQualities(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = artistContext.createUnmarshaller();
		Artist artist2 = null;
		ARTIST = ("./" + name + ".xml");
		artist2 = (Artist) um.unmarshal(new FileReader(
				ARTIST));
		
		int[] chordQualities = artist2.getChordQualities();
		return chordQualities;
	}
	
	
	public static int[] getChordDurations(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = artistContext.createUnmarshaller();
		Artist artist2 = null;
		ARTIST = ("./" + name + ".xml");
		artist2 = (Artist) um.unmarshal(new FileReader(
				ARTIST));
		
		int[] durations = artist2.getChordDurations();
		return durations;
	}

//	---------------------- xml Song info: ---------------------------------------------------------------------


	public void writeSongStyle() throws JAXBException, IOException {
		// create JAXB context and instantiate marshaller
		Marshaller m = songContext.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(song, System.out);
		Writer w = null;
		try {
			w = new FileWriter(SONG);
			m.marshal(song, w);
		} finally {
			try {
				w.close();
			} catch (Exception e) {
			}
		}
	}

	public boolean readSongStyle(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = songContext.createUnmarshaller();
		SONG = ("./" + name + ".xml");
		boolean exists = true;

		try {
			song = (Song) um.unmarshal(new FileReader(
					SONG));

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			exists = false;
			// e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			exists = false;
			// e.printStackTrace();
		}
		return exists;
	}

	
	public void newSong(String name) throws JAXBException, IOException {
		songContext = JAXBContext.newInstance(Song.class);
		song.setName(name);
		SONG = ("./" + name + ".xml");
	}
	
	public void setSongKey(int key) {
		song.setKey(key);
	}
	
	public void setSongIsMinor(boolean isMinor) {
		song.setIsMinor(isMinor);
	}
	
	public void setSongMeter(int meter) {
		song.setMeter(meter);
	}
	
	public void setSongMelodyPitch(int[][] melodyPitch) {
		song.setMelodyPitch(melodyPitch);
	}
	
	public void setSongMelodyOnset(int[][] melodyOnset) {
		song.setMelodyOnset(melodyOnset);
	}
	
	public void setSongChordRoots(int[] chordRoots) {
		song.setChordRoots(chordRoots);
	}
	
	public void setSongChordQualities(int[] chordQualities) {
		song.setChordQualities(chordQualities);
	}
	
	public void setSongOnsets(int[] onsets) {
		song.setOnsets(onsets);
	}
	
	public void setSongDurations(int[] durations) {
			song.setDurations(durations);
	}
	
	public static int getSongKey(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = songContext.createUnmarshaller();
		Song song = null;
		SONG = ("./" + name + ".xml");
		song = (Song) um.unmarshal(new FileReader(
				SONG));
		
		int key = song.getKey();
		return key;
	}
	
	public static boolean getSongIsMinor(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = songContext.createUnmarshaller();
		Song song = null;
		SONG = ("./" + name + ".xml");
		song = (Song) um.unmarshal(new FileReader(
				SONG));
		
		boolean isMinor = song.getIsMinor();
		return isMinor;
	}
	
	public static int getSongMeter(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = songContext.createUnmarshaller();
		Song song = null;
		SONG = ("./" + name + ".xml");
		song = (Song) um.unmarshal(new FileReader(
				SONG));
		
		int meter = song.getMeter();
		return meter;
	}
	
	public static int[] getSongChordRoots(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = songContext.createUnmarshaller();
		Song song = null;
		SONG = ("./" + name + ".xml");
		song = (Song) um.unmarshal(new FileReader(
				SONG));
		
		int[] chordRoots = song.getChordRoots();
		return chordRoots;
	}
	
	public static int[] getSongChordQualities(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = songContext.createUnmarshaller();
		Song song = null;
		SONG = ("./" + name + ".xml");
		song = (Song) um.unmarshal(new FileReader(
				SONG));
		
		int[] chordQualities = song.getChordQualities();
		return chordQualities;
	}
	
	public static int[] getSongOnsets(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = songContext.createUnmarshaller();
		Song song = null;
		SONG = ("./" + name + ".xml");
		song = (Song) um.unmarshal(new FileReader(
				SONG));
		
		int[] onsets = song.getOnsets();
		return onsets;
	}
	
	public static int[] getSongDurations(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = songContext.createUnmarshaller();
		Song song = null;
		SONG = ("./" + name + ".xml");
		song = (Song) um.unmarshal(new FileReader(
				SONG));
		
		int[] durations = song.getDurations();
		return durations;
	}
	
	public static int[][] getSongMelodyPitch(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = songContext.createUnmarshaller();
		Song song = null;
		SONG = ("./" + name + ".xml");
		song = (Song) um.unmarshal(new FileReader(
				SONG));
		
		int[][] melodyPitch = song.getMelodyPitch();
		return melodyPitch;
	}
	
	public static int[][] getSongMelodyOnset(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = songContext.createUnmarshaller();
		Song song = null;
		SONG = ("./" + name + ".xml");
		song = (Song) um.unmarshal(new FileReader(
				SONG));
		
		int[][] melodyOnset = song.getMelodyOnset();
		return melodyOnset;
	}
	

//	---------------------- xml Score info: ---------------------------------------------------------------------

	public void writeScore() throws JAXBException, IOException {
		// create JAXB context and instantiate marshaller
		Marshaller m = scoreContext.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(score, System.out);
		Writer w = null;
		try {
			w = new FileWriter(SCORE);
			m.marshal(score, w);
		} finally {
			try {
				w.close();
			} catch (Exception e) {
			}
		}
	}
	
	public boolean readScore(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = scoreContext.createUnmarshaller();
		SCORE = ("./" + name + ".xml");
		boolean exists = true;

		try {
			score = (Score) um.unmarshal(new FileReader(
					SCORE));

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			exists = false;
			// e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			exists = false;
			// e.printStackTrace();
		}
		return exists;
	}
	
	
	public void newScore(String name) throws JAXBException, IOException {
		scoreContext = JAXBContext.newInstance(Score.class);
		score.setName(name);
		SCORE = ("./" + name + ".xml");
	}
	
	public void setScoreLoad(String[][] load) {
		score.setLoad(load);
	}

	public static String[][] getScoreLoad(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = scoreContext.createUnmarshaller();
		Score score = null;
		SCORE = ("./" + name + ".xml");
		score = (Score) um.unmarshal(new FileReader(
				SCORE));
		
		String[][] load = score.getLoad();
		return load;
	}
	
	public void setScoreInstructions(int[][] instructions) {
		score.setInstructions(instructions);
	}

	public static int[][] getScoreInstructions(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = scoreContext.createUnmarshaller();
		Score score = null;
		SCORE = ("./" + name + ".xml");
		score = (Score) um.unmarshal(new FileReader(
				SCORE));
		
		int[][] instructions = score.getInstructions();
		return instructions;
	}


//	---------------------- Style: ---------------------------------------------------------------------

	
	public void newStyle(String name) throws JAXBException, IOException {
		styleContext = JAXBContext.newInstance(Style.class);
		style.setName(name);
		STYLE = ("./" + name + ".xml");
	}
	
	public void writeStyle() throws JAXBException, IOException {
		// create JAXB context and instantiate marshaller
		Marshaller m = styleContext.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(style, System.out);
		Writer w = null;
		try {
			w = new FileWriter(STYLE);
			m.marshal(style, w);
		} finally {
			try {
				w.close();
			} catch (Exception e) {
			}
		}
	}
	
	public boolean readStyle(String name) throws JAXBException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = styleContext.createUnmarshaller();
		STYLE = ("./" + name + ".xml");
		boolean exists = true;

		try {
			style = (Style) um.unmarshal(new FileReader(
					STYLE));

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			exists = false;
			// e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			exists = false;
			// e.printStackTrace();
		}
		return exists;
	}
	
	public void setStyleArtists(String[] artists) {
		style.setArtists(artists);
	}

	public static String[] getArtists(String name) throws JAXBException, FileNotFoundException {
		// get variables from our xml file, created before
		Unmarshaller um = null;
		um = styleContext.createUnmarshaller();
		Style style = null;
		STYLE = ("./" + name + ".xml");
		style = (Style) um.unmarshal(new FileReader(
				STYLE));
		
		String[] artists = style.getArtists();
		return artists;
	}
}

